package com.team.game.Game.Wizards.FireWizard;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Matrix;
import android.graphics.PointF;
import android.graphics.RectF;
import android.view.MotionEvent;

import com.team.game.Game.Impacts;
import com.team.game.Game.Levels;
import com.team.game.framework.Abstract.GesturesInfo;
import com.team.game.framework.Abstract.Spell;
import com.team.game.framework.Game;
import com.team.game.framework.impl.MainActivity;
import com.team.game.framework.impl.Scale_X_Y;
import com.team.game.framework.impl.SurfaceThread;

import java.io.IOException;
import java.io.InputStream;

import static com.team.game.framework.impl.Scale_X_Y.Scale_X;
import static com.team.game.framework.impl.Scale_X_Y.Scale_Y;
import static com.team.game.framework.impl.Scale_X_Y.relation;
import static com.team.game.framework.impl.Scale_X_Y.scale;

public class Fire_Circle extends Spell {

    private Bitmap dragon;
    private RectF rectF;
    private PointF pointF;
    private Matrix matrix;

    private float radius, dem;

    public Fire_Circle () {
        super ();

        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/hero_1/spell_2.png");
            icon_spell = BitmapFactory.decodeStream (inputStream);
        } catch (IOException e) {
            e.printStackTrace ();
        }
        cost = new int[]{20, 30, 40, 50};
    }

    public Fire_Circle (Game game) {
        super (game);

        // нужно делать в каждом скиле, устанавливаем посицию кружка перезагрузки, вершину ( она будет меняться) и время перезагрузки
        ProgressBarRect.set(19 * Scale_X_Y.scale, 113 * Scale_X_Y.scale, 69 * Scale_X_Y.scale, 163 * Scale_X_Y.scale);
        RectDstForBlackProgress.set(21 * Scale_X_Y.scale, 115 * Scale_X_Y.scale, 67 * Scale_X_Y.scale, 161 * Scale_X_Y.scale);
        TopOfRectProgressDst = RectDstForBlackProgress.top;
        TimeRecharch = 8;
        WaitForTap = false;

        rectF = new RectF ();
        pointF = new PointF ();
        matrix = new Matrix ();
    }

    float k = 0;

    @Override
    public boolean updateSpell () {
        time += SurfaceThread.deltaTime;
        if (time < 3) {
            k = SurfaceThread.deltaTime * 187.5f;


            matrix.reset();
            matrix.preScale((rectF.width() / dragon.getWidth()), (rectF.height() / dragon.getHeight()));
            matrix.postTranslate(rectF.left, rectF.top);

            matrix.postRotate(-time * 187.5f, pointF.x, pointF.y);


            //game.getCurrentScreen().getLevel().impactOnTheRadius(pointF, dem * (k / 144), radius);
            Impacts.impactOnTheRadius (level, pointF, dem * (k / 144), radius);


        }
        if (time > TimeRecharch) {
            this.restart();
            return false;
        }
        return true;
    }

    @Override
    public boolean activeSpell (GesturesInfo gesturesInfo1) {

        matrix.reset ();
        matrix.setTranslate (10000, 10000);
        rectF.set (gesturesInfo1.gesture.getBoundingBox ());
        rectF.set (rectF.left, rectF.centerY () - rectF.width () / 2,
                rectF.right, rectF.centerY () + rectF.width () / 2);

        pointF.set (rectF.centerX (), rectF.centerY ());
        radius = ((rectF.height () / 2) + (rectF.width () / 2)) / 2;
        Active = true;

//        rectF.set (gesturesInfo1.gesture.getBoundingBox ());
//        rectF.set ((pointF.x + radius/2)*scale,
//                   (pointF.y + radius/2)*scale,
//                  (pointF.x + radius/2)*scale,
//                (pointF.y + radius/2)*scale);

        //TODO Пересчитать урон
        //чем больше круг тем меньше урон
        //но в большом круг они находятся дольше
        //нужно найти правильную формулу
        dem = (540 * scale) / radius;

        level = (Levels) game.getCurrentScreen ().getLevel ();


        return false;
    }

    @Override
    public boolean setMotionEventSpell (MotionEvent motionEvent) {
        return false;
    }

    //Paint paint = new Paint();

    @Override
    public void drawSpell (Canvas canvas) {
        if (time < 3)
            canvas.drawBitmap(dragon, matrix, null);

        //тест точки вращения
//    paint.setColor (Color.GREEN);
//    paint.setStrokeWidth (10.0f);
//    canvas.drawPoint (pointF.x, pointF.y, paint);
        //тест радиуса круга
//    paint.setStyle (Paint.Style.STROKE);
//    canvas.drawCircle (pointF.x , pointF.y, radius, paint);

    }

    @Override
    public void restart () {

        this.progressBarRestart();
        matrix.reset();
        matrix.setTranslate(10000, 10000);
    }

    @Override
    public void dispose () {

    }

    @Override
    public void setImages () {
        InputStream inputStream;
        try {
            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Circle/Dragon.png");
            dragon = BitmapFactory.decodeStream (inputStream);

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Circle/Circle_Normal.png");
            ProgressBarNormal = (BitmapFactory.decodeStream (inputStream));

            inputStream = MainActivity.manager.open ("Picture/Gestures/FireMage/Circle/Circle_Active.png");
            ProgressBarActive = (BitmapFactory.decodeStream (inputStream));

        } catch (IOException e) {
            e.printStackTrace ();
        }

        SPELL_NAME = "fire_circle";
    }

    @Override
    public void drowProfileExample (Canvas canvas, int A) {
        paintText.setTextSize (30 * relation);
        paintText.setColor (Color.RED);
        canvas.drawText ("Working on it\n\n\n\nDamage:?/?/?/?", 660 * Scale_X, 900 * Scale_Y, paintText);
    }

    @Override
    public void updateProfileExample() {

    }

    @Override
    public void setStartConfiguration() {

    }
}
